package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
import com.ncst.backend.domain.MedicalTechnology;
import com.ncst.backend.service.MedicalTechnologyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 医技项目控制器
 */
@Api(tags = "医技项目管理")
@Slf4j
@RestController
@RequestMapping("/medical-technology")
public class MedicalTechnologyController {

    @Autowired
    private MedicalTechnologyService medicalTechnologyService;

    @ApiOperation("根据ID查询医技项目")
    @GetMapping("/{id}")
    public Result<MedicalTechnology> getById(@ApiParam("医技项目ID") @PathVariable Integer id) {
        MedicalTechnology medicalTechnology = medicalTechnologyService.getById(id);
        return medicalTechnology != null ? Result.success(medicalTechnology) : Result.notFound("医技项目不存在");
    }

    @ApiOperation("查询所有医技项目")
    @GetMapping
    public Result<List<MedicalTechnology>> getAll() {
        return Result.success(medicalTechnologyService.getAll());
    }

    /**
     * 查询处置项目
     * @return
     */
    @ApiOperation("查询处置项目")
    @GetMapping("/chuZhi")
    public Result<List<MedicalTechnology>> getChuZhi() {
        return Result.success(medicalTechnologyService.getChuZhi());
    }
    @ApiOperation("根据条件查询医技项目列表")
    @PostMapping("/search")
    public Result<List<MedicalTechnology>> getByCondition(@ApiParam("查询条件") @RequestBody MedicalTechnology medicalTechnology) {
        return Result.success(medicalTechnologyService.getByCondition(medicalTechnology));
    }

    @ApiOperation("根据名称查询医技项目")
    @GetMapping("/name/{name}")
    public Result<List<MedicalTechnology>> getByName(@ApiParam("项目名称") @PathVariable String name) {
        return Result.success(medicalTechnologyService.getByName(name));
    }

    @ApiOperation("分页查询医技项目")
    @GetMapping("/page")
    public Result<List<MedicalTechnology>> getByPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(medicalTechnologyService.getByPage(pageNum, pageSize));
    }

    @ApiOperation("带条件分页查询医技项目")
    @PostMapping("/page")
    public Result<List<MedicalTechnology>> getByPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("查询条件") @RequestBody MedicalTechnology medicalTechnology) {
        return Result.success(medicalTechnologyService.getByPage(pageNum, pageSize, medicalTechnology));
    }

    @ApiOperation("统计医技项目总数")
    @GetMapping("/count")
    public Result<Long> countAll() {
        return Result.success(medicalTechnologyService.countAll());
    }

    @ApiOperation("根据条件统计医技项目数量")
    @PostMapping("/count")
    public Result<Long> countByCondition(@ApiParam("查询条件") @RequestBody MedicalTechnology medicalTechnology) {
        return Result.success(medicalTechnologyService.countByCondition(medicalTechnology));
    }

    @ApiOperation("新增医技项目")
    @PostMapping
    public Result<Void> save(@ApiParam("医技项目信息") @RequestBody MedicalTechnology medicalTechnology) {
        return medicalTechnologyService.save(medicalTechnology) ? Result.success("新增成功") : Result.error("新增失败");
    }

    @ApiOperation("批量新增医技项目")
    @PostMapping("/batch")
    public Result<Void> saveBatch(@ApiParam("医技项目列表") @RequestBody List<MedicalTechnology> list) {
        return medicalTechnologyService.saveBatch(list) ? Result.success("批量新增成功") : Result.error("批量新增失败");
    }

    @ApiOperation("根据ID更新医技项目")
    @PutMapping("/{id}")
    public Result<Void> updateById(@ApiParam("医技项目ID") @PathVariable Integer id, @ApiParam("医技项目信息") @RequestBody MedicalTechnology medicalTechnology) {
        medicalTechnology.setId(id);
        return medicalTechnologyService.updateById(medicalTechnology) ? Result.success("更新成功") : Result.error("更新失败");
    }

    @ApiOperation("根据ID删除医技项目")
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@ApiParam("医技项目ID") @PathVariable Integer id) {
        return medicalTechnologyService.removeById(id) ? Result.success("删除成功") : Result.error("删除失败");
    }

    @ApiOperation("批量删除医技项目")
    @DeleteMapping("/batch")
    public Result<Void> removeByIds(@ApiParam("医技项目ID列表") @RequestBody List<Integer> ids) {
        return medicalTechnologyService.removeByIds(ids) ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }
}
